<template>
  <div class="home">
    <div class="com" style="min-height: 440px">
      <Banner :banner="bannerList" />
    </div>
    <div class="section">
      <div class="com" style="min-height: 410px">
        <Index-Company :company="companyList" />
      </div>
      <div class="com" style="min-height: 870px">
        <Index-Job :hotjob="hotjobList" />
      </div>
    </div>
  </div>
</template>

<script>
import { homePage } from '@/api/home';
import { setUser } from '@/utils/cookie';
// import { getUser } from '@/utils/cookie';
export default {
  // middleware: 'auth',
  asyncData ({ params, error }) {
    // return homePage({}).then(res => {
    //   console.log(res)
    //   if (res.code === 200) {
    //     return { info: res.data }
    //   } else {
    //     this.$message.error(res.msg)
    //     return { info: {} }
    //   }
    // }).catch(err => {
    //   console.log(err)
    //   error({ statusCode: 404, message: err })
    // })
  },
  components: {},
  data () {
    return {
      info: {},
      bannerList: [],
      companyList: [],
      hotjobList: []
    }
  },
  head () {
    return {
      titleTemplate: (titleChunk) => {
        return `${titleChunk} - 大学生找工作、找实习`;
      }
    }
  },
  created () {
  },
  mounted () {
    // const USER = getUser() || '';
    // USER && !this.$store.state.user.authUser && this.$store.dispatch('user/userInfo', JSON.parse(USER))
    this.getHomePage();
  },
  watch: {

  },
  computed: {

  },
  methods: {
    getHomePage () {
      homePage({}).then(res => {
        if (res.code === 200) {
          this.info = res.data;
          res.data.banner && (this.bannerList = res.data.banner);
          res.data.company && (this.companyList = res.data.company);
          res.data.hotjob && (this.hotjobList = res.data.hotjob);
          // 获取最新用户信息
          if (res.data.user_info) {
            res.data.user_info.checked = this.$store.state.user.authUser.checked || false;
            this.$store.dispatch('user/userInfo', res.data.user_info);
            if (this.$store.state.user.authUser.checked) {
              setUser(JSON.stringify(res.data.user_info), 30)
            } else {
              setUser(JSON.stringify(res.data.user_info));
            }
          }
        } else if (res.code === 1008) {
          console.log('请登录')
        } else {
          this.$message.error(res.msg)
        }
      }).catch(err => {
        console.log(err)
      })
    },
  }
}
</script>

<style lang="scss" scoped>
.home {
  .section {
    width: $base-width;
    margin: 0 auto;
  }
}
</style>
